package ajo;

import static org.junit.Assert.assertTrue;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IBulletSnapshot;
import robocode.control.testing.RobotTestBed;

/**
 * Tests that BattleBot uses bullet power proportional to it's distance from the enemy.
 * 
 * @author Joshua Antonio
 */
public class TestBattleBotFire extends RobotTestBed {

  boolean firePowerReachLevelOne = false;
  boolean firePowerReachLevelTwo = false;
  boolean firePowerReachLevelThree = false;

  /**
   * Specifies that Crazy and BattleBot are to be matched up in this test case.
   * 
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {

    return "sample.Crazy,ajo.BattleBot";
  }

  /**
   * This test runs for 10 rounds.
   * 
   * @return The number of rounds.
   */
  @Override
  public int getNumRounds() {

    return 10;
  }

  /**
   * At the end of each turn, checks the power of all bullets moving across the battlefield. Checks
   * to see if there is any variability in bullet power, proving that BattleBot is using
   * proportional firing based on distance of detected enemy robot.
   * 
   * @param event Info about the current state of the battle.
   */
  @Override
  public void onTurnEnded(TurnEndedEvent event) {

    // All active bullets belong to BattleBot since SittingDuck does not fire.
    IBulletSnapshot bullets[] = event.getTurnSnapshot().getBullets();
    double bulletPower;

    for (int i = 0; i < bullets.length; i++) {

      bulletPower = bullets[i].getPower();

      if (bulletPower < 1.5) {
        firePowerReachLevelOne = true;
      }
      else if (bulletPower >= 1.5 && bulletPower < 2.8) {
        firePowerReachLevelTwo = true;
      }
      else if (bulletPower >= 2.8) {
        firePowerReachLevelThree = true;
      }
    }
  }

  /**
   * After running all matches, determine if BattleBot has had variability in its bullet power.
   * 
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {

    assertTrue("Bullet Power less than 1.5", firePowerReachLevelOne);
    assertTrue("Bullet Power between 1.5 and 2.8", firePowerReachLevelTwo);
    assertTrue("Bullet Power greater than 2.8", firePowerReachLevelThree);
  }
}